package love.unknown.fruit.controller;

import love.unknown.fruit.dao.FruitDao;
import love.unknown.fruit.dao.impl.FruitDAOImpl;
import love.unknown.fruit.pojo.Fruit;

import java.util.List;
import java.util.Scanner;

//菜单类
public class Menu {
    Scanner sc = new Scanner(System.in);
    FruitDao fruitDao = new FruitDAOImpl();

    //显示主菜单
    public int showMainMenu() {

        System.out.println("=====================欢迎使用水果库存系统=====================");
        System.out.println("1.查看水果库存列表");
        System.out.println("2.添加水果库存信息");
        System.out.println("3.查看特定水果库存信息");
        System.out.println("4.水果下架");
        System.out.println("5.退出");
        System.out.println("==========================================================");
        System.out.print("请选择：");
        return sc.nextInt();
    }

    //查看水果库存列表
    public void showFruitList() {
        List<Fruit> fruitList = fruitDao.getFruitList();
        System.out.println("==========================================================");
        System.out.println("编号\t\t名称\t\t单价\t\t库存\t\t备注");
        if (fruitList == null || fruitList.size() <= 0){
            System.out.println("对不起,库存为空!");
        }else {
            for (Fruit fruit:
                    fruitList) {
                System.out.println(fruit);
            }
        }


        System.out.println("==========================================================");
    }

    //添加水果库存信息  --业务方法：添加库存记录
    public void addFruit(){
        System.out.println("请输入水果名称：");
        String fname = sc.next();
        Fruit fruit = fruitDao.getFruitByName(fname);
        if (fruit==null){   //添加
            System.out.println("请输入水果单价：");
            int price = sc.nextInt();
            System.out.println("请输入水果库存量：");
            int fcount = sc.nextInt();
            System.out.println("请输入水果备注：");
            String remark = sc.next();
            //封装Fruit对象
            fruit = new Fruit(0, fname, price, fcount, remark);
            //调用DAO的添加方法
            fruitDao.addFruit(fruit);
        }else {     //修改
            System.out.println("请输入追加的库存量：");
            int fcount = sc.nextInt();
            fruit.setFcount(fruit.getFcount()+fcount);
            //调用DAO修改方法
            fruitDao.updateFruit(fruit);
        }
    }

    //查看指定水果信息
    public void showFruitInfo(){
        System.out.println("请输入水果名称：");
        String fname = sc.next();
        Fruit fruit = fruitDao.getFruitByName(fname);
        if (fruit==null){
            System.out.println("对不起,没有查到相关记录!");
        }else {
            System.out.println("==========================================================");
            System.out.println("编号\t\t名称\t\t单价\t\t库存\t\t备注");
            System.out.println(fruit);
            System.out.println("==========================================================");
        }
    }

    //水果下架
    public void delFruit(){
        System.out.println("请输入水果名称：");
        String fname = sc.next();
        Fruit fruit = fruitDao.getFruitByName(fname);
        if (fruit==null){
            System.out.println("对不起，没有相关信息!");
        }else {
            System.out.println("是否确认下架？(Y/N)");
            String slt = sc.next();
            if ("y".equalsIgnoreCase(slt)){
                fruitDao.delFruit(fname);
                System.out.println("下架成功!");
            }
        }
    }

    //退出
    public boolean exit() {
        System.out.print("是否确认退出？(Y/N)");
        String slt = this.sc.next();
        return !"Y".equalsIgnoreCase(slt);
    }
}
